package com.icom.sso.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.xxl.sso.server.core.model.SysUser;

@Mapper
public interface SysUserMapper {
	// @Select("select su.*,sd.dpt_name from sys_user su left join sys_department sd
	// on su.dept_id = sd.id where su.status = 1 and su.is_admin != 1")
	@Select({ "<script>", "select su.*,sd.dpt_name from sys_user su left join sys_department sd  on su.dept_id = sd.id",
			" where su.status = 1 and su.is_admin != 1 ", "<when test='deptId!=null'> ", "and ((su.dept_id in ( ",
			"select id from (select *,if(find_in_set(pid, @pids) > 0, @pids := concat(@pids, ',', id), 0) as ischild ",
			"from ( select * from sys_department t where t.status = 1 ) t1,(select @pids := #{deptId}) t2) t3",
			" where STATUS = 1 and ischild != 0 )) or su.dept_id = #{deptId}) ", "</when>", "<when test='name!=null'> ",
			"and name like '%${name}%' ", "</when>", "<when test='login!=null'> ", "and login_id like '%${login}%' ",
			"</when>", "</script>" })
	@Results({ @Result(id = true, column = "id", property = "id"), @Result(column = "name", property = "name"),
			@Result(column = "login_id", property = "loginId"), @Result(column = "login_pwd", property = "password"),
			@Result(column = "is_admin", property = "isAdmin"), @Result(column = "dpt_name", property = "department") })
	List<SysUser> findUsers(@Param("deptId") String deptId, @Param("name") String name, @Param("login") String login);

	@Select("select * from sys_user limit #{page} , #{rows}")
	List<SysUser> pageSysUser(@Param("page") int page, @Param("rows") int rows);

	@Select("select * from sys_user where login_id = #{userName} and status = 1")
	@Results({ @Result(id = true, column = "id", property = "id"), @Result(column = "name", property = "name"),
			@Result(column = "login_id", property = "loginId"), @Result(column = "login_pwd", property = "password"),
			@Result(column = "is_admin", property = "isAdmin"), @Result(column = "dept_id", property = "deptId") })
	SysUser findUserByLoginId(@Param("userName") String userName);

	@Update("update sys_user set login_pwd = #{password} where login_id = #{loginId}")
	int editUser(@Param("loginId") String loginId, @Param("password") String password);

	@Insert("insert into sys_user(name, login_id, login_pwd,dept_id,status) values(#{name}, #{loginId}, #{password},#{deptId} ,1)")
	int insertUser(SysUser user);

	@Select("select * from sys_user where id = #{id} and status = 1")
	@Results({ @Result(id = true, column = "id", property = "id"), @Result(column = "name", property = "name"),
			@Result(column = "login_id", property = "loginId"), @Result(column = "login_pwd", property = "password"),
			@Result(column = "is_admin", property = "isAdmin"), @Result(column = "dept_id", property = "deptId") })
	SysUser findUserById(@Param("id") String id);

	@Update("update sys_user set status = 0 where id = #{id}")
	int delUser(@Param("id") String id);

	@Update("update sys_user set login_pwd = #{pwd} where id = #{id}")
	int resetUserPwd(@Param("id") String id, @Param("pwd") String pwd);
	
	@Update("update sys_user set dept_id = #{deptId} where id = #{id}")
	int saveEditInfo(SysUser sysUser);
}
